<?php
namespace app\admin666\controller;

use think\Controller;
use think\Request;

class Admin extends Controller{
    
    protected $rules = [];
    
    /* 空操作，用于输出404页面 */
    public function _empty() {
        return '404';
    }
    
    /**
    * 初始化
    * @date: 2018年6月6日 下午3:53:27
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function _initialize(Request $request = null){
        define('NOW_TIME',time());
        define('MODULE_NAME', Request::instance()->module());
        define('CONTROLLER_NAME', Request::instance()->controller());
        define('ACTION_NAME', Request::instance()->action());
        
        $base    = request()->root();
        $root    = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
        if ('' != $root) {
            $root = '/' . ltrim($root, '/');
        }
        
        config('web_path',$root.'/');
        
        // 获取当前用户ID
        define('UID',is_login());
        
        if( !UID ){
            // 还没登录 跳转到登录页面
            $this->redirect('Publics/login');
        }
        
        /* 读取数据库中的配置 */
        $config =   cache('DB_CONFIG_DATA');
        if(!$config){
            $config =  config_lists();
            cache('DB_CONFIG_DATA',$config);
        }
        config($config); //添加配置
        
        $this->assign('vars',$config);
        
        // 是否是超级管理员
        define('IS_ROOT', 1);
        
        $user_auth = session('user_auth');
        $this->assign($user_auth);
        
        $uinfo = model('Member')->get(UID);
        $this->assign('uinfo',$uinfo);
        
        define('GID',$uinfo['group_id']);
        
        if(GID > 0){
            $ginfo = model('AuthGroup')->find(GID);
            $this->rules = explode(',', $ginfo['rules']);
        }
        $this->assign('_rules',$this->rules);
        
        $menus = $this->getMenus();
        $this->assign('__MENU__', $menus);
        
        if(!empty($menus)){
            action_log($menus['cid']);
        }
        
        if ( !$this->checkRule($menus['cid']) ){
            $this->error('未授权访问!');
        }
        
        if($uinfo['status']==0){
            $url  = strtolower(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
            $notChkRule = [
                'admin/index/index'
            ];
            if(!in_array($url, $notChkRule)){
                $this->error('你的账号已被禁用!');
            }
        }
    }
    
    /**
    * 权限
    * @date: 2018年6月6日 下午4:06:14
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    final protected function checkRule($rule){
        if(UID == IS_ROOT){
            return true;
        }
        
        if(empty($rule)){
            $url  = strtolower(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
            $notChkRule = [
                'admin/index/pwd',
                'admin/index/info',
                'admin/index/main',
                'admin/system/cache',
                'admin/ajax/chkuser',
                'admin/index/index',
                'admin/member/status',
                'admin/file/upload',
            ];
            if(in_array($url, $notChkRule)){
                return true;
            }
        }
        
        if(in_array($rule, $this->rules)){
            return true;
        }
        
        return false;
    }
    
    
    /**
    * 菜单
    * @date: 2018年6月6日 下午4:03:07
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    final public function getMenus($controller=CONTROLLER_NAME){
        if(empty($menus)){
            $menus = [];
            // 获取主菜单
            $where['pid']   =   0;
            $where['hide']  =   0;
            $where['type'] = 1;
            if(!config('is_dev')){ // 是否开发者模式
                $where['is_dev']    =   0;
            }
            $menus  =   model('Menu')->where($where)->order('sort asc')->select();
    
            //高亮主菜单
            $current = model('Menu')->where("type=1 and url = '{$controller}/".ACTION_NAME."'")->field('id,pid,title')->find();
            $cid = $current['pid']>0?$current['pid']:$current['id'];
            foreach($menus as $key=>$val){
                // 判断主菜单权限
                if (!$this->checkRule(intval($val['id'])) ) {
                    unset($menus[$key]);
                    continue;//继续循环
                }
    
                $map['pid'] =   $val['id'];
                $map['hide']    =   0;
                if(!config('is_dev')){ // 是否开发者模式
                    $map['is_dev']  =   0;
                }
    
                $child = model('Menu')->where($map)->order('sort asc')->select();
    
                foreach ($child as $k=>$v){
                    // 判断子菜单权限
                    if (!$this->checkRule(intval($v['id']))) {
                        unset($child[$k]);
                        continue;//继续循环
                    }
                }
    
                $menus[$key]['child'] =  $child;
            }
        }
        return ['menu'=>$menus,'cid'=>$current['id'],'pid'=>$current['pid'],'menuname'=>$current['title']];
    
    }
    
}